package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;

/**
 * 我发布     我收藏
 * 我的兴趣墙发布 改界面fragment 为{@link InterestMainFragment} 内的一个子fragment.与其他四个{@link WaterFallFragment}属于
 * 同一个FragmentStateAdapter
 * @author crj
 */
public class InterestUserRelease extends Fragment implements OnClickListener{
//	public final String TAG = "InterestUserRelease";
//	public final boolean RELEASE =true;
//	public final boolean COLLECT = false;
	private View releaseItem,collectItem;
//	private ViewPager mViewPager;
	//我发布 次数显示
	private TextView releaseNumText;
	//我收藏次数显示
	private TextView collectNumText;
	//标志位 为RELEASE代表为我发布界面，如果为COLLECT为我收藏界面
//	private boolean flag = RELEASE;
//	private boolean isClick = false;
	private Fragment mFragment1;//已发布
	private Fragment mFragment2;//已收藏
	private int tabId;
	
	public static InterestUserRelease newInstance(int position) {
		InterestUserRelease f = new InterestUserRelease();
		Bundle mBundle = new Bundle();
		mBundle.putInt("position", position);
		f.setArguments(mBundle);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState != null){
			tabId = savedInstanceState.getInt("tabid");
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.interest_user_release, container, false);
		
		initView(v);
		
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		onChangeTab(tabId);
		
		getUserReleaseCount(SmartFoxClient.getLoginUserId());
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("tabid",tabId);
	}
	
	public void onChangeTab(int id){
		if(id == 0)id = R.id.releaseitem;
		if(id == R.id.releaseitem){
			releaseItem.setSelected(true);
			collectItem.setSelected(false);
		}else{
			releaseItem.setSelected(false);
			collectItem.setSelected(true);
		}
		if(tabId == id)return;
		FragmentManager fg = getChildFragmentManager();
		FragmentTransaction ft = fg.beginTransaction();
		Fragment fragment = fg.findFragmentById(R.id.fragmentcontent);
		if(fragment != null){
			ft.detach(fragment);
		}
		if(id == R.id.releaseitem){//发布
			if(mFragment1 == null){
				fragment = WaterFallFragment.newInstance(0,2,"0","0");
				ft.add(R.id.fragmentcontent,fragment);
				mFragment1 = fragment;
			}else{
				ft.attach(mFragment1);
			}
		}else{//收藏
			if(mFragment2 == null){
				fragment = WaterFallFragment.newInstance(1,2,"0","0");
				ft.add(R.id.fragmentcontent,fragment);
				mFragment2 = fragment;
			}else{
				ft.attach(mFragment2);
			}
		}
		ft.commitAllowingStateLoss();
		tabId = id;
	}
	
	public void initView(View v) {
		//初始化上边两个item
		releaseItem = v.findViewById(R.id.releaseitem);
		releaseNumText = (TextView) releaseItem.findViewById(R.id.releasenum);
		releaseItem.setOnClickListener(this);
		//收藏item
		collectItem = v.findViewById(R.id.collectitem);
		collectNumText = (TextView) collectItem.findViewById(R.id.releasenum);
		collectItem.setOnClickListener(this);
	}
	
	public void refreshData(int picid) {
		getUserReleaseCount(SmartFoxClient.getLoginUserId());
	}
	
	private void getUserReleaseCount(final String cusid) {
		HttpRestClient.doHttpRequestUserReleaseCount(cusid, 
				new ObjectHttpResponseHandler(getActivity()) {
			@Override
			public Object onParseResponse(String cotent) {
				return parseJson(cotent);
			}
			@Override
			public void onSuccess(int statusCode, Object response) {
				List<String> data = (List<String>) response;
				if(data.size()>0) {
					releaseNumText.setText(data.get(0));
					collectNumText.setText(data.get(1));
				}
			}
		});
	}
	
	/**
	 * 我发布我收藏json数据解析
	 * @param json
	 * @return
	 */
	/*  {"fabuCount":1,"shoucCount":3} */
	public List<String> parseJson(String json) {
		List<String> result = new ArrayList<String>(); 
		try {
			JSONObject jsonObject = new JSONObject(json);
			result.add(jsonObject.getString("fabuCount"));
			 result.add(jsonObject.getString("shoucCount"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public void onResume() {
		onChangeTab(tabId);
		getUserReleaseCount(SmartFoxClient.getLoginUserId());
		super.onResume();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.releaseitem:
			onChangeTab(R.id.releaseitem);
			getUserReleaseCount(SmartFoxClient.getLoginUserId());
			break;
		case R.id.collectitem:
			onChangeTab(R.id.collectitem);
			break;
	/*	case R.id.title_right:
			Intent intent = new Intent(this.getActivity(), InterestReleaseTopic.class);
			this.startActivity(intent);
			break;*/
		}
	}

	
}
